package com.awakening.dao.Impl;

import com.awakening.dao.UserDao;
import com.awakening.pojo.User;
import com.awakening.utils.DataSourceUtils;
import com.awakening.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * 用户Dao实现类
 * @Author:AwakeningCode
 * @Date: Created in 17:55 2018\1\22 0022
 */
public class UserDaoImpl implements UserDao{

    //DAO中保存用户的方法
    public void regist(User user) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "insert into regist_web values(?,?,?,?,?,?,?)";
        Object[] params = {user.getUid(),user.getUsername(),user.getPassword(),user.getNickname(),user.getEmail(),
                user.getState(),user.getCode()};
        queryRunner.update(sql,params);
    }

    public User findByCode(String code) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select * from regist_web where code = ?";
        User user = queryRunner.query(sql,new BeanHandler<User>(User.class),code);
        return user;
    }

    public void update(User user) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "update regist_web set username=?,password=?,nickname=?,email=?,state=?,code=? where uid=?";
        Object[] params = {user.getUsername(),user.getPassword(),user.getNickname(),user.getEmail(),
                user.getState(),user.getCode(),user.getUid()};
        queryRunner.update(sql,params);
    }

}
